<?php

class RouteTracker extends Zend_Controller_Plugin_Abstract {

    public function __construct() {
        $this->_registry = Zend_Registry::getInstance();
    }

    public function routeStartup(Zend_Controller_Request_Abstract $request) {
        
    }

    public function routeShutdown(Zend_Controller_Request_Abstract $request) {
        
    }

    public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request) {
        
    }

    public function preDispatch(Zend_Controller_Request_Abstract $request) {
                       
        $redirect = new Zend_Controller_Action_Helper_Redirector();
        $auth = Zend_Auth::getInstance();
        $moduleName = $request->getModuleName();
        if ($auth->hasIdentity()) {

            $userDetail = $auth->getIdentity();
            if ($userDetail->role == 'user') {
                switch ($userDetail->department) {
                    case 'planning':                       
                        break;
                    case 'marketing':
                        ($moduleName == 'marketing' || $moduleName == 'user')?'':$redirect->gotoUrl('marketing/mynotification');
                        break;
                    case 'streetDigging':
                        ($moduleName == 'street' || $moduleName == 'user')?'':$redirect->gotoUrl('street/mynotification');
                        break;
                    case 'houseDigging':
                        ($moduleName == 'house' || $moduleName == 'user')?'':$redirect->gotoUrl('house/mynotification');
                        break;
                    case 'installation':
                        ($moduleName == 'installation' || $moduleName == 'user')?'':$redirect->gotoUrl('installation/mynotification');
                        break;
                    case 'economy':
                        ($moduleName == 'economy' || $moduleName == 'user')?'':$redirect->gotoUrl('economy/');
                        break;
                    default:
                        break;
                }            
            }
            if ($userDetail->role == 'admin') {
                switch ($userDetail->department) {
                    case 'planning':                       
                        break;
                    case 'marketing':
                        ($moduleName == 'marketing' || $moduleName == 'user')?'':$redirect->gotoUrl('marketing/notification');
                        break;
                    case 'streetDigging':
                        ($moduleName == 'street' || $moduleName == 'user')?'':$redirect->gotoUrl('street/notification');
                        break;
                    case 'houseDigging':
                        ($moduleName == 'house' || $moduleName == 'user')?'':$redirect->gotoUrl('house/notification');
                        break;
                    case 'installation':
                        ($moduleName == 'installation' || $moduleName == 'user')?'':$redirect->gotoUrl('installation/notification');
                        break;
                    case 'economy':
                        ($moduleName == 'economy' || $moduleName == 'user')?'':$redirect->gotoUrl('economy/');
                        break;
                    default:
                        break;
                }            
            }
        }
    }

    public function postDispatch(Zend_Controller_Request_Abstract $request) {
        
    }

    public function dispatchLoopShutdown() {
        
    }

}

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {

    protected function _initDoctype() {

        $configuration = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini', 'production');

        $dbAdapter = Zend_Db::factory($configuration->database);
        Zend_Db_Table_Abstract::setDefaultAdapter($dbAdapter);

        $this->_registry = Zend_Registry::getInstance();
        $this->_registry->configuration = $configuration;
        $this->_registry->dbAdapter = $dbAdapter;

        $ctrl = Zend_Controller_Front::getInstance();
        $ctrl->registerPlugin(new RouteTracker());

        $view = new Zend_View();
        $view->addHelperPath('../application/helpers/');
        $view->addScriptPath('../application/views/scripts/');

        $view->headTitle('crm');
        $view->headLink()->appendStylesheet($view->baseURI() . 'css/style.css');
        $view->headLink()->appendStylesheet($view->baseURI() . 'css/jquery-ui.css');
        $view->headScript()->appendFile($view->baseURI() . 'js/jquery.js');
        $view->headScript()->appendFile($view->baseURI() . 'js/jquery-ui.js');

        $router = Zend_Controller_Front::getInstance()->getRouter();
        $route = new Zend_Controller_Router_Route(':module/:controller/:action/:id/:page', array('module' => '', 'controller' => '', 'action' => '', 'id' => '0', 'page' => '0'));
        $router->addRoute('default', $route);

        $route = new Zend_Controller_Router_Route('/marketing/target/:user/:switch/:page', array('module' => 'marketing', 'controller' => 'target', 'action' => 'index', 'user' => '0', 'switch' => '0', 'page' => '0'));
        $router->addRoute('target-index', $route);

        $route = new Zend_Controller_Router_Route('/marketing/target/index/:user/:switch/:page', array('module' => 'marketing', 'controller' => 'target', 'action' => 'index', 'user' => '0', 'switch' => '0', 'page' => '0'));
        $router->addRoute('target-index2', $route);

        $route = new Zend_Controller_Router_Route('/marketing/target/list/:user/:switch/:page', array('module' => 'marketing', 'controller' => 'target', 'action' => 'list', 'user' => '0', 'switch' => '0', 'page' => '0'));
        $router->addRoute('target-listing', $route);

        $route = new Zend_Controller_Router_Route('/marketing/target/add/:user/:switch', array('module' => 'marketing', 'controller' => 'target', 'action' => 'add', 'user' => '0', 'switch' => '0'));
        $router->addRoute('add-target', $route);

        $route = new Zend_Controller_Router_Route('/marketing/target/edit/:id/:user/:switch', array('module' => 'marketing', 'controller' => 'target', 'action' => 'edit', 'id' => '0', 'user' => '0', 'switch' => '0'));
        $router->addRoute('edit-target', $route);

        $route = new Zend_Controller_Router_Route('/marketing/document/download/:id', array('module' => 'marketing', 'controller' => 'document', 'action' => 'download', 'id' => '0'));
        $router->addRoute('download_marketing_file', $route);

        $moduleName = Zend_Controller_Front::getInstance()->getRequest();
        //print_r($moduleName);
    }

}

